package com.quanyan.stadium.web.api;

import com.quanyan.api.APIResponse;
import com.quanyan.stadium.api.req.ReqCategoryPriorityList;
import com.quanyan.stadium.api.req.ReqGameType;
import com.quanyan.stadium.api.req.ReqId;
import com.quanyan.stadium.api.resp.RespGameSportCardData;
import com.quanyan.stadium.api.resp.RespUserCategoryList;
import com.quanyan.stadium.service.SportCardService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author dongshenglin
 * @date 2016/6/30  12:01
 */
@RestController
@RequestMapping("/api/stadium/sportCard")
public class SportCardController {

    private final static Logger logger = LoggerFactory.getLogger(SportCardController.class);

    @Autowired
    private SportCardService sportCardService;

    @RequestMapping(value = "/getUserCategoryList",method = RequestMethod.POST)
    public APIResponse<RespUserCategoryList> getUserCategoryList(@RequestBody ReqId ids,BindingResult result){
        if(result.hasErrors()) {
            return APIResponse.returnFail(result.getFieldError().getDefaultMessage());
        }
        return sportCardService.getUserCategoryList(ids.getUid());
    }

    @RequestMapping(value = "/setCategoryPriority",method = RequestMethod.POST)
    public APIResponse setCategoryPriority(@RequestBody @Valid ReqCategoryPriorityList reqCategoryPriorityList,HttpServletRequest request){
        reqCategoryPriorityList.setUid((Integer)request.getAttribute("uid"));
        return APIResponse.returnSuccess(sportCardService.setCategoryPriority(reqCategoryPriorityList));
    }

    @RequestMapping(value = "/getSportCardData",method = RequestMethod.POST)
    public APIResponse<RespGameSportCardData> getSportCardData(@RequestBody ReqGameType reqGameType){
        Integer categoryId = reqGameType.getCategoryId()!=null?reqGameType.getCategoryId():reqGameType.getGameType();
        if (categoryId  == null) {
            categoryId = 1;
        }
        return sportCardService.getSportCardData(reqGameType.getUid(),categoryId);
    }

}
